package pe.com.upc.edu.rtt.msg;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.liferay.portal.kernel.util.PropsUtil;
 
public class MessageSender {
 
    private static final String URL =  PropsUtil.get("ws.server.activeMQ.host.map.mensajeria");
 
    private static final String USER = ActiveMQConnection.DEFAULT_USER;
 
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
 
    private static final String DESTINATION_QUEUE =  PropsUtil.get("ws.server.activeMQ.queue");
    
    private static final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USER, PASSWORD, URL);
 
    private static final boolean TRANSACTED_SESSION = true;
 
    public static void sendMessages(String tipo, String keymensaje,String uuid, String emailuser,String strobjMensaje) throws JMSException { 
        Connection connection = connectionFactory.createConnection();
        connection.start(); 
        final Session session = connection.createSession(TRANSACTED_SESSION, Session.AUTO_ACKNOWLEDGE);
        final Destination destination = session.createQueue(DESTINATION_QUEUE); 
        final MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT); 
        MapMessage mapmensgw = session.createMapMessage();
        mapmensgw.setString("tipo", tipo);
        mapmensgw.setString("emailuser", emailuser);
        mapmensgw.setString("userPortal", uuid);
        mapmensgw.setString(keymensaje, strobjMensaje) ;
        sendMessage(mapmensgw,producer);
        session.commit(); 
        session.close();
        connection.close(); 
    }
 
    private static void sendMessage(final MapMessage message, MessageProducer producer) throws JMSException {
        producer.send(message);
    }

   
}